package gsz;

import org.junit.Test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @create by: gsz
 * @since : 2022/8/25
 * 联系方式：郭苏忠 13042040757
 */
@SuppressWarnings("all")
public class ShopMain {
    /**
     * 1、有一家超市，出售苹果和草莓。其中苹果 8 元/斤，草莓 13 元/斤。
     * 现在顾客 A 在超市购买了若干斤苹果和草莓，需要计算一共多少钱？
     * 请编写函数，对于 A 购买的水果斤数 (水果斤数为大于等于 0 的整数)，计算并返回所购买商品的总价。
     */
    @Test
    public void test1() {
        ShopMain shopMain = new ShopMain();
        ArrayList<Fruit> fruits = new ArrayList<>();
        shopMain.shop(Apple.class, "8", "1", fruits);
        shopMain.shop(Strawberry.class, "13", "1", fruits);
        BigDecimal resultPrice = shopMain.BuyFruit(fruits);
        System.out.println(resultPrice);
    }

    /**
     * 2、超市增加了一种水果芒果，其定价为 20 元/斤。
     * 现在顾客 B 在超市购买了若干斤苹果、 草莓和芒果，需计算一共需要多少钱？
     * 请编写函数，对于 B 购买的水果斤数 (水果斤数为大于等于 0 的整数)，计算并返回所购买商品的总价。
     */
    @Test
    public void test2() {
        ShopMain shopMain = new ShopMain();
        ArrayList<Fruit> fruits = new ArrayList<>();
        shopMain.shop(Apple.class, "8", "1", fruits);
        shopMain.shop(Strawberry.class, "13", "1", fruits);
        shopMain.shop(Mango.class, "20", "1", fruits);
        BigDecimal resultPrice = shopMain.BuyFruit(fruits);
        System.out.println(resultPrice);
    }

    /**
     * 3、超市做促销活动，草莓限时打 8 折。
     * 现在顾客 C 在超市购买了若干斤苹果、 草莓和芒果，需计算一共需要多少钱？
     * 请编写函数，对于 C 购买的水果斤数 (水果斤数为大于等于 0 的整数)，计算并返回所购买商品的总价。
     */
    @Test
    public void test3() {
        ShopMain shopMain = new ShopMain();
        ArrayList<Fruit> fruits = new ArrayList<>();
        shopMain.shop(Apple.class, "8", "1", fruits);
        shopMain.shop(Strawberry.class, "13", "1", "0.8", fruits);
        shopMain.shop(Mango.class, "20", "1", fruits);
        BigDecimal resultPrice = shopMain.BuyFruit(fruits);
        System.out.println(resultPrice);
    }

    /**
     * 4、促销活动效果明显，超市继续加大促销力度，购物满 100 减 10 块。
     * 现在顾客 D 在超市购买了若干斤苹果、 草莓和芒果，需计算一共需要多少钱？
     * 请编写函数，对于 C 购买的水果斤数 (水果斤数为大于等于 0 的整数)，计算并返回所购买商品的总价。
     */
    @Test
    public void test4() {
        ShopMain shopMain = new ShopMain();
        ArrayList<Fruit> fruits = new ArrayList<>();
        shopMain.shop(Apple.class, "8", "2.5","0.4",fruits);
        shopMain.shop(Strawberry.class, "13", "3.5", "0.8", fruits);
        shopMain.shop(Mango.class, "100", "1.1", fruits);
        BigDecimal manJiantPrice = shopMain.BuyFruit(fruits, ManJian.Man100Jian10);
    }

    public List<Fruit> shop(Class clazz, String price, String jin, List<Fruit> fruits) {
        return shop(clazz, price, jin, "1", fruits);
    }

    public List<Fruit> shop(Class clazz, String price, String jin, String discount, List<Fruit> fruits) {
        return shop(clazz, new BigDecimal(price), new BigDecimal(jin), new BigDecimal(discount), fruits);
    }

    public List<Fruit> shop(Class clazz, BigDecimal price, BigDecimal jin, List<Fruit> fruits) {
        return shop(clazz, price, jin, new BigDecimal("1"), fruits);
    }

    /**
     * @param clazz    购买水果的类型
     * @param price    单价，每元/斤
     * @param jin      斤数
     * @param discount 折扣
     * @param fruits   水果集合
     * @return
     */
    public List<Fruit> shop(Class clazz, BigDecimal price, BigDecimal jin, BigDecimal discount, List<Fruit> fruits) {
        if (price != null && !(price.compareTo(BigDecimal.valueOf(0)) > -1)) {
            throw new RuntimeException("请输入正确的价格!");
        }
        if (jin != null && !(jin.compareTo(BigDecimal.valueOf(0)) > -1)) {
            throw new RuntimeException("请输入正确的斤数!");
        }
        if (discount != null && !(discount.compareTo(BigDecimal.valueOf(0)) > -1)) {
            throw new RuntimeException("请输入正确的折扣,如0.8表示8折!");
        }
        if (clazz.getSuperclass().isAssignableFrom(Fruit.class)) {
            Fruit fruit = null;
            try {
                fruit = (Fruit) clazz.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
            fruit.setPrice(price);
            fruit.setJin(jin);
            fruit.setDiscount(discount);
            if (clazz.isAssignableFrom(Apple.class)) {
                Apple apple = (Apple) fruit;
                apple.setName("苹果");
                fruits.add(apple);
            }
            if (clazz.isAssignableFrom(Mango.class)) {
                Mango mango = (Mango) fruit;
                mango.setName("芒果");
                fruits.add(mango);
            }
            if (clazz.isAssignableFrom(Strawberry.class)) {
                Strawberry strawberry = (Strawberry) fruit;
                strawberry.setName("草莓");
                fruits.add(strawberry);
            }
        }
        return fruits;
    }

    public BigDecimal BuyFruit(List<Fruit> fruits) {
        return BuyFruit(fruits, null);
    }

    public BigDecimal BuyFruit(List<Fruit> fruits, ManJian manJian) {
        if (fruits.size() < 0) {
            throw new RuntimeException("请购买水果再结算");
        }
        BigDecimal resultPrice = BigDecimal.ZERO;
        for (int i = 0; i < fruits.size(); i++) {
            Fruit fruit = fruits.get(i);
            BigDecimal countPrice = fruit.getPrice().multiply(fruit.getJin()).multiply(fruit.getDiscount());
            System.out.println(fruit.getName()+":"+countPrice+"元");
            resultPrice = resultPrice.add(countPrice);
        }
        if (manJian != null) {
            BigDecimal man = manJian.getMan();
            //购买总价大于等于满减力度
            if (resultPrice.compareTo(man) > -1) {
                System.out.println("原价:"+resultPrice+"元");
                //购买总价减去折扣
                resultPrice = resultPrice.subtract(manJian.getJian());
                System.out.println("满减之后:"+resultPrice+"元");
            }
        }
        return resultPrice;
    }


}
